package com.lucker.controller.pagead;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.lucker.controller.BaseController;
import com.lucker.ds.model.PageAd;
import com.lucker.framework.constant.AdPageName;
import com.lucker.framework.constant.ResponseBaseModel;
import com.lucker.framework.constant.ResponseResultModel;
import com.lucker.service.pagead.PageAdService;

@Controller
@RequestMapping("ad")
public class PageAdController extends BaseController{
	
	@Autowired
	PageAdService pageAdService;
	
	
	@RequestMapping(value="/index",method = RequestMethod.POST)
	public @ResponseBody ResponseEntity<?> commit_opencode(HttpServletRequest req) {
		List<Map<String,Object>> retList = new ArrayList<Map<String,Object>>();
		PageAd ad = new PageAd();
		ad.setViewPage(AdPageName.index_page);
		ad.setIsDelete("false");
		ad.orderByOrderIndex(true);
		ad.setLimitStart(0);
		ad.setLimitEnd(4);
		try {
			ad.setPageTarget("1");
			List<PageAd> banner_list = pageAdService.queryAll(ad);
			
			
			if(CollectionUtils.isNotEmpty(banner_list)) {
				Map<String,Object> banner_map = new HashMap<String,Object>();
				List<Object> _banner_list = new ArrayList<Object>();
				banner_map.put("moudleType", 1);
				Map<String,Object> _map = null;
				for(PageAd ads : banner_list) {
					_map = new HashMap<String,Object>();
					_map.put("title", ads.getSourceTitle());
					_map.put("desc", ads.getSourceDesc());
					_map.put("imgUrl", ads.getViewPic());
					_map.put("linkUrl", ads.getSkipUrl());
					_map.put("createTime", ads.getCreateTime().getTime());
					_banner_list.add(_map);
				}
				banner_map.put("moudleList", _banner_list);
				retList.add(banner_map);
			}
			
			
			
			ad.setPageTarget("2");
			List<PageAd> icon_list = pageAdService.queryAll(ad);
			
			if(CollectionUtils.isNotEmpty(icon_list)) {
				Map<String,Object> icon_map = new HashMap<String,Object>();
				List<Object> _icon_list = new ArrayList<Object>();
				icon_map.put("moudleType", 2);
				Map<String,Object> _map = null;
				for(PageAd ads : icon_list) {
					_map = new HashMap<String,Object>();
					_map.put("title", ads.getSourceTitle());
					_map.put("desc", ads.getSourceDesc());
					_map.put("imgUrl", ads.getViewPic());
					_map.put("linkUrl", ads.getSkipUrl());
					_map.put("createTime", ads.getCreateTime().getTime());
					_icon_list.add(_map);
				}
				icon_map.put("moudleList", _icon_list);
				retList.add(icon_map);
			}
			
			
			
			
			
			ad.setPageTarget("3");
			List<PageAd> topic_list = pageAdService.queryAll(ad);
			
			if(CollectionUtils.isNotEmpty(topic_list)) {
				Map<String,Object> topic_map = new HashMap<String,Object>();
				List<Object> _topic_list = new ArrayList<Object>();
				topic_map.put("moudleType", 3);
				Map<String,Object> _map = null;
				for(PageAd ads : topic_list) {
					_map = new HashMap<String,Object>();
					_map.put("title", ads.getSourceTitle());
					_map.put("desc", ads.getSourceDesc());
					_map.put("imgUrl", ads.getViewPic());
					_map.put("linkUrl", ads.getSkipUrl());
					_map.put("createTime", ads.getCreateTime().getTime());
					_topic_list.add(_map);
				}
				topic_map.put("moudleList", _topic_list);
				retList.add(topic_map);
			}
			
			
			return new ResponseEntity<>(new ResponseResultModel<List>(retList),HttpStatus.OK);
		}catch(Exception e) {
			this.getLoger().error("查询推荐信息异常 {} , {}", e.getMessage(),e);
			return new ResponseEntity<>(new ResponseBaseModel(29002,e.getMessage()), HttpStatus.OK);
		}
	}
	 
	
	
}
